package com.action;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.service.UserService;

@Controller
@Transactional
public class LoginAction extends ActionSupport {
	
	@Resource private UserService userservice;

	private static final long serialVersionUID = 1L;
	
	public String execute() {			
		ActionContext context = ActionContext.getContext();// 获取action上下文
		Map<String, Object> session = context.getSession(); //获取sesion
		//HttpServletResponse response = (HttpServletResponse)context.get(ServletActionContext.HTTP_RESPONSE);
		HttpServletRequest request=(HttpServletRequest)context.get(ServletActionContext.HTTP_REQUEST);
		String username=request.getParameter("username").toString();
		String password=request.getParameter("password").toString();
		String userclass=request.getParameter("userclass").toString();
		String sqlpass=userservice.find(username).getPassword();
		String sqlclass=userservice.find(username).getUserclass();
		if(sqlpass.equals(password) && sqlclass.equals(userclass)) {
			session.put("username", username);
			session.put("password", password);
			session.put("userclass", userclass);
			return SUCCESS;
		}
		else {
			String failure=userservice.find("This is failure and you can not find the username").getPassword();
			return failure;
		}		
	}	
}
